﻿using System;
using System.Collections.Generic;
using OpenLS.Core;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    class RangeFormatter
    {
        private readonly Workbook _workbook;
        //\\ private readonly StyleRecord _record;

        public RangeFormatter(FormattingRecord record, Range range)
        {
            _workbook = range.Worksheet.Workbook;
            //\\   _record = record;
            _protectionFormatter = new ProtectionFormatter(record);
            _bordersFormatter = new BordersFormatter(range);
            _FormatFormatter = new FormatFormatter();
            _fontFormatter = new FontFormatter(record);
            _backgroundFormatter = new BackgroundFormatter (record);
            _alignmentFormatter = new AlignmentFormatter();
        }

        public AlignmentFormatter AlignmentFormatter { get { return _alignmentFormatter; } }
        private readonly FontFormatter _fontFormatter;
        public FontFormatter FontFormatter
        {
            get { return _fontFormatter; }
        }
        BackgroundFormatter _backgroundFormatter ;
        public BackgroundFormatter BackgroundFormatter
        {
            get { return _backgroundFormatter; }
        }FormatFormatter _FormatFormatter = new FormatFormatter();
        public FormatFormatter FormatFormatter { get { return _FormatFormatter; } }

        private readonly BordersFormatter _bordersFormatter;
        private readonly ProtectionFormatter _protectionFormatter;
        private AlignmentFormatter _alignmentFormatter;

        public BordersFormatter BordersFormatter
        {
            get { return _bordersFormatter; }
        }
        public ProtectionFormatter ProtectionFormatter { get { return _protectionFormatter; } }

        public FormattingRecord Record
        {
            get { throw new NotImplementedException();}
        }

        internal void Apply(Range selection)
        {
            bool isDirty = Enumerable.Any(GetFormatters(), f => f.IsDirty);
            if (!isDirty)
                return;
            using (selection.Worksheet.Workbook.CreateUndoScope(UIStringTable.Format))
            {

                foreach (var formatter in GetFormatters())
                {
                    formatter.Apply(selection);
                }
            }
        }
        IEnumerable<FormatterBase> GetFormatters()
        {
                ; yield return FormatFormatter;
           ; yield return ProtectionFormatter;
           ; yield return BordersFormatter;

           ; yield return this.AlignmentFormatter;
           ; yield return this.FontFormatter;
           ; yield return BackgroundFormatter;
            ;
        }
    }
}